#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_BT_PHONE	(0)
#define ITEM_BT_BOOK	(1)
#define ITEM_BT_SET		(2)

#define ITEM_BTMAIN_ITEM_CALL		(3)
#define ITEM_BTMAIN_ITEM_1			(4)
#define ITEM_BTMAIN_ITEM_2			(5)
#define ITEM_BTMAIN_ITEM_3		(6)
#define ITEM_BTMAIN_ITEM_C			(7)
#define ITEM_BTMAIN_ITEM_4			(8)
#define ITEM_BTMAIN_ITEM_5			(9)
#define ITEM_BTMAIN_ITEM_6			(10)
#define ITEM_BTMAIN_ITEM_UNCALL			(11)
#define ITEM_BTMAIN_ITEM_7		(12)
#define ITEM_BTMAIN_ITEM_8			(13)
#define ITEM_BTMAIN_ITEM_9		(14)
#define ITEM_BTMAIN_ITEM_B			(15)
#define ITEM_BTMAIN_ITEM_STAR		(16)
#define ITEM_BTMAIN_ITEM_0			(17)
#define ITEM_BTMAIN_ITEM_ENT			(18)

#define ITEM_BTCALL_ITEM_BTBUT		(3)
#define ITEM_BTCALL_ITEM_CALBUT		(4)
#define ITEM_BTCALL_ITEM_UNCAL		(5)

#define ITEM_BTBOOK_MAIN_DIALED			(3)
#define ITEM_BTBOOK_MAIN_RECEV			(4)
#define ITEM_BTBOOK_MAIN_MISSED			(5)

#define IMG_BTBOOK_NUM_DIALED			(3)
#define IMG_BTBOOK_NUM_RECEV				(4)
#define IMG_BTBOOK_NUM_MISSED			(5)

#define ITEM_BTTOOL_PAIR_ENTER			(3)
#define ITEM_BTTOOL_PAIR_EXIT			(4)
#define ITEM_BTTOOL_ATANSR_ON           (5)
#define ITEM_BTTOOL_ATANSR_OFF 			(6)
#define ITEM_BTTOOL_ATLINK_ON			(7)
#define ITEM_BTTOOL_ATLINK_OFF          (8)

#define DISP_ALL 0xaa


TRect		BTMainTouchRect[IMG_BTMAIN_COM_ITEM_QTY]= {{8,150,144,246}, {8,252,144,348}, {8,354,144,450} };
TRect		BTMainDailTouchRect[IMG_BTMAIN_KEY_ITEM_QTY-3]= {	{176,150,297,226}, {298,150,419,226}, {420,150,541,226},{541,150,662,226},\
											{176,226,297,302}, {298,226,419,302}, {420,226,541,302},{541,226,662,302},\
											{176,302,297,378}, {298,302,419,378}, {420,302,541,378},{541,302,662,378},\
											{176,378,297,454}, {298,378,419,454}, {420,378,541,454},{541,378,662,454},};
TRect		BTCallTouchRect[IMG_BTCALL_ITEM_QTY-3]= {{247,373,332,426}, {371,373,456,426}, {494,373,579,426} };
TRect		BTBookNumTouchRect[IMG_BTBOOK_NUM_ITEM_QTY-3]= {{260,180,632,223}, {260,233,632,276}, {260,286,632,329},{260,340,632,383}, {260,394,632,437}};
TRect		BTToolDispRect[3]= {{244,194,454,234}, {244,290,454,330}, {244,372,454,412} };
TRect		BTToolTouchRect[IMG_BTTOOL_ITEM_QTY-3]= {{490,176,594,207}, {490,214,594,245}, {490,266,590,297},{490,304,594,330}, {490,356,594,387}, {490,394,594,425}};
TRect		BTCallComingDispRect = {276,180,566,230};
TRect		BTCallConnectDispRect = {210,280,300,420};
TRect		CoverInfoRect = {176,150,660,452};
TPosCoor	CoverInfoPos= {176,150};
TPosCoor	BTDispBarInfoPos= {176,88};
TPosCoor	CallBarInfoPos= {397,475};

int			MenuBTFocus = -1;
TFrame		MenuBTFrame;
char* 		BOOKMAINStr[3] = {"DIALED CALLS","RECEIVED CALLS","MISSED CALLS"};
char* 		BTCALLStr[2] = {"COMING CALL","CONNECTING"};
char* 		BTTOOLInfoStr[3] = {"PAIR","AUTO ANSWER","AUTO LINK"};
char* 		BTTOOLButStr[6] = {"ENTER","EXIT","ON","OFF","ON","OFF"};
char*		BOOKMAINBARStr= {"Download Dialed Calls"};
char		PhoneNO [5][20]= {"","","","",""};
char		ComingCallNO [20]= {""};
char*		BTStatusCmd[16] = {"","waiting","Disconnect","Pairing","Linking","Connect","Ring","Dialing",\
							"Talking","Call Failed","Miss call","Call release","MP3 play","Call setup","A2DP connected","A2DP disconnected"};
unsigned char	DailedNO [20]= {""};

static 	unsigned char BTDisp_Flag = 0;//0 is BTmain, 1 is BTcall, 2 is BTbook_main, 3 is BTbook_num,4 is BTtool
static int TRANSFER_AUD =0;//0 is to cell ;1 is to BT
int Status_flag =0; //  1 is talking.
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern  unsigned char send_data_buf[5];
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern int 	Last_ID ;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;


static int send_data (int data_3,int length);



int  BTInfoUpdata(void)
{	
	int   i;
	static int phone_book_count =0;

	switch (read_info_data[2])
		{
			case	ID_BT_STATUS:				
				if (read_info_data[3]==ID_SCREEN_BT_TALK) 
					Status_flag =1;
				else
					Status_flag =0;
				break;
				
			case	ID_COMEING_CALL_NUM:
				for(i=3; i<=read_info_data[2];i++)
				ComingCallNO[i-3]= read_info_data[i];
				break;
			case	ID_DIALED_CALL_NUM:
			case	ID_RECEIVED_CALL_NUM:
			case	ID_MISSED_CALL_NUM:
				for(i=3; i<=read_info_data[2];i++)
					PhoneNO [phone_book_count][i-3] = read_info_data[i];
				phone_book_count ++;
				if (phone_book_count ==5)
					phone_book_count = 0;
				break;
		}	
	 
}




void BTDispDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;		
					printf ("y is %d \n",y);
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "BLUE TOOTH" );
				DrawJpeg(pCanvas, 640, 22, "/res_jpeg/btsign.jpg" );	
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				BTDispDrawVolume(MenuBTFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}


void BTDispDrawBackgroup(TCanvas *pCanvas)
{	
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );	
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "BLUE TOOTH" );
	DrawJpeg(pCanvas, 640, 22, "/res_jpeg/btsign.jpg" );	
}


void BTDispDrawComItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{	
	char FileName[20];
	int GetJpegIdx;

	if (idx < 0 || idx > IMG_BTMAIN_COM_ITEM_QTY )
		return;
	
	if( bfocus ){
			GetJpegIdx = IMG_BTMAIN_ITEM_PHONE_FOCUS+idx ;
		}else{
			GetJpegIdx = IMG_BTMAIN_ITEM_PHONE+idx ;
		}
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, BTMainTouchRect[idx].left, BTMainTouchRect[idx].top, FileName);

}
void BTDispDrawBarInfoItem( TCanvas *pCanvas ,char * str,BOOL bfocus )
{	

	char FileName[20];
	int GetJpegIdx;
	TJpeg pJpeg;
	TRect DispBarRect;
	TPosCoor pos;

printf ("in draw bar.bfocus is %d\n",bfocus);
	GetJpegIdx = IMG_BTMAIN_ITEM_NUMBAR;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, BTDispBarInfoPos.x, BTDispBarInfoPos.y, FileName);	
	pos = BTDispBarInfoPos;			
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
	pJpeg = GetJpegPtr(FileName);
	DispBarRect.left = pos.x;
	DispBarRect.right = pos.x + pJpeg.Width;
	DispBarRect.top = pos.y;
	DispBarRect.bottom = pos.y + pJpeg.Height;
	if( bfocus ){
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );
		   	g_Puts( pCanvas, FONT33, DispBarRect, TEXT_ALIGN_LEFT, str);
		}
	dt_Present_part(DispBarRect);
}

void BTDispDrawKeyItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{	
	char FileName[20];
	int GetJpegIdx;
	int i;
	printf (" in draw key pad. idx is %d,bfocus is %d \n",idx,bfocus);

	if ( idx < 3 || (idx >= IMG_BTMAIN_KEY_ITEM_QTY && idx != DISP_ALL ) )
		return;
	if (idx == DISP_ALL)
		{
			for (i=0; i<IMG_BTMAIN_KEY_ITEM_QTY-3; i++)
				{
				GetJpegIdx = IMG_BTMAIN_ITEM_CALL+i ;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
				DrawJpeg(pCanvas, BTMainDailTouchRect[i].left, BTMainDailTouchRect[i].top, FileName);
				}
			
			return;
		}
	
	if( bfocus ){
			GetJpegIdx = IMG_BTMAIN_ITEM_CALL_FOCUS+idx-3 ;
		}else{
			GetJpegIdx = IMG_BTMAIN_ITEM_CALL+idx-3 ;
		}		
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, BTMainDailTouchRect[idx-3].left, BTMainDailTouchRect[idx-3].top, FileName);
}


void BTDispDrawBTCallItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{	
	char FileName[20];
	int GetJpegIdx;
	int i;

	if( idx < 3 || (idx >= IMG_BTCALL_ITEM_QTY && idx != DISP_ALL ) )
		return;
	if (idx == DISP_ALL )
		{		
			GetJpegIdx = IMG_BTMAIN_ITEM_COVER_DISP;			
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, CoverInfoPos.x, CoverInfoPos.y, FileName);
			
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );
		   	g_Puts( pCanvas, FONT33, BTCallComingDispRect, TEXT_ALIGN_CENTER, BTCALLStr[0]);
		   	g_Puts( pCanvas, FONT33, BTCallComingDispRect, TEXT_ALIGN_CENTER, BTCALLStr[1]);
			GetJpegIdx = IMG_BTCALL_ITEM_CALLBAR;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, CallBarInfoPos.x, CallBarInfoPos.y, FileName);	
	
			for (i=0; i<IMG_BTCALL_ITEM_QTY-3; i++)
				{
				GetJpegIdx = IMG_BTCALL_ITEM_BTBUT+i ;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
				DrawJpeg(pCanvas, BTCallTouchRect[i].left, BTCallTouchRect[i].top, FileName);
				}
			return;
		}
	
	if( bfocus ){
			GetJpegIdx = IMG_BTCALL_ITEM_BTBUT+idx-3 ;
		}else{
			GetJpegIdx = IMG_BTCALL_ITEM_BTBUT_FOCUS+idx-3 ;
		}		
		sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
		DrawJpeg(pCanvas, BTCallTouchRect[idx-3].left, BTCallTouchRect[idx-3].top, FileName);
}


void BTDispDrawBTBookMainItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{	
	char FileName[20];
	TJpeg pJpeg;
	int GetJpegIdx;
	TRect DispBarRect;
	TPosCoor pos;
	int i;

	if( idx < 3 || (idx >= IMG_BTBOOK_MAIN_ITEM_QTY&& idx != DISP_ALL ) )
		return;
	if (idx == DISP_ALL )
		{
			GetJpegIdx = IMG_BTMAIN_ITEM_NUMBAR;
			pos = BTDispBarInfoPos;			
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			pJpeg = GetJpegPtr(FileName);
			DrawJpeg(pCanvas, pos.x, pos.y, FileName);
			DispBarRect.left = pos.x;
			DispBarRect.right = pos.x + pJpeg.Width;
			DispBarRect.top = pos.y;
			DispBarRect.bottom = pos.y + pJpeg.Height;
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );			
		   	g_Puts( pCanvas, FONT33, DispBarRect, TEXT_ALIGN_CENTER, BOOKMAINBARStr);

			GetJpegIdx = IMG_BTBOOK_MAIN_COVER_DISP;
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, CoverInfoPos.x, CoverInfoPos.y, FileName);	
			
			for (i=0; i<IMG_BTBOOK_MAIN_ITEM_QTY-3; i++)
				{				
				g_Puts( pCanvas, FONT33, BTBookNumTouchRect[i], TEXT_ALIGN_CENTER, BOOKMAINStr[i]);
				
				}
			printf ("in BTDispDrawBTBookMainItem __1211\n");
			return;
		}
	
	if( bfocus ){
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_YELLOW, 0, 0 );	
		}else{
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );	
		}		
		g_Puts( pCanvas, FONT33, BTBookNumTouchRect[idx-3], TEXT_ALIGN_CENTER, BOOKMAINStr[idx-3]);
}


void BTDispDrawBTBookNumItem( TCanvas *pCanvas, int idx, BOOL bfocus )
	{	
		char FileName[20];
		int GetJpegIdx;
		int i;
	
		if( idx < 3 || idx > (IMG_BTBOOK_NUM_ITEM_QTY&& idx != DISP_ALL ) )
			return;
		if (idx == DISP_ALL )
		{
			GetJpegIdx = IMG_BTBOOK_NUM_COVER_DISP;			
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, CoverInfoPos.x, CoverInfoPos.y, FileName);

			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );			
			for (i=0; i<IMG_BTBOOK_NUM_ITEM_QTY-3; i++)
				{	
				if (PhoneNO[i][0] !=' ')
					g_Puts( pCanvas, FONT33, BTBookNumTouchRect[i], TEXT_ALIGN_CENTER, PhoneNO[i]);
				else
					break;
				}
			return;
		}
	
	if( bfocus ){
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_YELLOW, 0, 0 );	
		}else{
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );	
		}		
		g_Puts( pCanvas, FONT33, BTBookNumTouchRect[idx-3], TEXT_ALIGN_CENTER, PhoneNO[idx-3]);
	}


void BTDispDrawBTToolItem( TCanvas *pCanvas, int idx, BOOL bfocus )
	{	
		char FileName[20];
		int GetJpegIdx;
		int i;
	
		if( idx < 3 || (idx >= IMG_BTTOOL_ITEM_QTY&& idx != DISP_ALL ) )
			return;
		if (idx == DISP_ALL )
		{
			GetJpegIdx = IMG_BTTOOL_COVER_DISP;			
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_BT_FOLDER, GetJpegIdx);
			DrawJpeg(pCanvas, CoverInfoPos.x, CoverInfoPos.y, FileName);

			g_SetTextFormat( FONT18, TEXT_NORMAL, RGB_BLACK, 0, 0 );
			g_Puts( pCanvas, FONT18, BTToolDispRect[0], TEXT_ALIGN_LEFT, BTTOOLInfoStr[0]);
			g_Puts( pCanvas, FONT18, BTToolDispRect[1], TEXT_ALIGN_LEFT, BTTOOLInfoStr[1]);
			g_Puts( pCanvas, FONT18, BTToolDispRect[2], TEXT_ALIGN_LEFT, BTTOOLInfoStr[2]);
			for (i=0; i<IMG_BTTOOL_ITEM_QTY-3; i++)
				{				
					g_Puts( pCanvas, FONT18, BTToolTouchRect[i], TEXT_ALIGN_CENTER, BTTOOLButStr[i]);
				}
			return;
		}
	
	if( bfocus ){
			g_SetTextFormat( FONT18, TEXT_NORMAL, RGB_YELLOW, 0, 0 );	
		}else{
			g_SetTextFormat( FONT18, TEXT_NORMAL, RGB_BLACK, 0, 0 );	
		}		
		g_Puts( pCanvas, FONT18, BTToolTouchRect[idx-3], TEXT_ALIGN_CENTER, BTTOOLButStr[idx-3]);
	}

void BTDispDrawSeparateItem( BOOL bDrawAll, int idx)
{
	Status_flag =0;
	switch (BTDisp_Flag)
		{
			case 0:
				BTDispDrawKeyItem( MenuBTFrame.pCanvas,idx,bDrawAll);
				break;
			case 1:
				BTDispDrawBTCallItem( MenuBTFrame.pCanvas,idx,bDrawAll);
				break;
			case 2:
				BTDispDrawBTBookMainItem( MenuBTFrame.pCanvas,idx,bDrawAll);
				break;
			case 3:
				BTDispDrawBTBookNumItem( MenuBTFrame.pCanvas,idx,bDrawAll);
				break;
			case 4:
				BTDispDrawBTToolItem( MenuBTFrame.pCanvas,idx,bDrawAll);
				break;						
				
		}
}


void BTDispDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		BTDispDrawBackgroup(MenuBTFrame.pCanvas);

	for( i = 0; i < IMG_BTMAIN_COM_ITEM_QTY; i++ )
		BTDispDrawComItem( MenuBTFrame.pCanvas, i, FALSE );
	BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,"",FALSE );
	BTDispDrawSeparateItem(FALSE,DISP_ALL);
	
		if (mute_status == 1)
		{
			BTDispDrawVolume(MenuBTFrame.pCanvas,1);		
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}


int BTMainGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_BTMAIN_COM_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, BTMainTouchRect[i] ) )
			return i;
	
	return -1;
}

int BTOtherGetTouchID( TPosCoor pos )
{
	int i;
	int ButQty =0;

		switch (BTDisp_Flag)
		{
			case 0:
				ButQty = IMG_BTMAIN_KEY_ITEM_QTY;
				for( i = 0; i < ButQty-3; i++ )
				if( ut_PtInRect( pos, BTMainDailTouchRect[i] ) )
					return i+3;	
				break;
			case 1:
				ButQty = IMG_BTCALL_ITEM_QTY;
				for( i = 0; i < ButQty-3; i++ )
				if( ut_PtInRect( pos, BTCallTouchRect[i] ) )
					return i+3;	
				break;
			case 2:
				ButQty = IMG_BTBOOK_MAIN_ITEM_QTY;
				for( i = 0; i < ButQty-3; i++ )
				if( ut_PtInRect( pos, BTBookNumTouchRect[i] ) )
					return i+3;	
				break;
			case 3:
				ButQty = IMG_BTBOOK_NUM_ITEM_QTY;
				for( i = 3; i < ButQty; i++ )
				if( ut_PtInRect( pos, BTBookNumTouchRect[i] ) )
					return i+3;	
				break;
			case 4:
				ButQty = IMG_BTTOOL_ITEM_QTY;
				for( i = 0; i < ButQty-3; i++ )
				if( ut_PtInRect( pos, BTToolTouchRect[i] ) )
					return i+3;	
				break;				
				
		}			
	return -1;
}

BOOL BTMainDailBtnEnter (int idx)
{
	static unsigned char length=0;	
	int SendData =0;	
	
  	if ((idx <3) ||(idx >= IMG_BTMAIN_KEY_ITEM_QTY))
		return -1;
	
	switch (idx)
				{
					case	ITEM_BTMAIN_ITEM_CALL:						
						Status_flag =1;
						//SendData = FUNC_KEY_DIAL;						
						send_data_buf[0]= HEAD;
						send_data_buf[1]= length +2;
						send_data_buf[2]= FUNC_CALL_NUM;
						int i;
						printf ("send phone No. length  is %d\n",length);
						for (i =0; i<length; i++)
							{
							send_data_buf[3+i]= DailedNO [i];							
							}
						return (send_data_out(send_data_buf,send_data_buf[1]+2 ));
						length =0;	
						break;
			
					case	ITEM_BTMAIN_ITEM_C:							
							length=0;
							memset(DailedNO, 0, sizeof(DailedNO));
							BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,"",FALSE );
							if (Status_flag )
							{
							SendData = FUNC_KEY_END;//FUNC_KEY_DEL;
							return (send_data(SendData,LENGTH));
							}
						break;
			
					case	ITEM_BTMAIN_ITEM_UNCALL:
						Status_flag =0;
						length=0;	
						memset(DailedNO, 0, sizeof(DailedNO));
						BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,"",FALSE );
						SendData = FUNC_KEY_END;
						return (send_data(SendData,LENGTH));
						break;
					case ITEM_BTMAIN_ITEM_B:
						if (TRANSFER_AUD)
							{
							TRANSFER_AUD =0;
							SendData= FUNC_KEY_TRANSFER_AUD_TO_BT;
							}
						else
							{
							TRANSFER_AUD =1;
							SendData = FUNC_KEY_TRANSFER_AUD_TO_CELL;
							}	
						return (send_data(SendData,LENGTH));
						break;
					case ITEM_BTMAIN_ITEM_STAR:
						DailedNO[i] = '*' ;
						length++;
						if (Status_flag )
							{
							return (send_data(BT_KEY_STAR,LENGTH));
							}	
						
						break;
					case	ITEM_BTMAIN_ITEM_ENT:	
						DailedNO[i] = '#' ;
						length++;
						if (Status_flag )
							{
							SendData = BT_KEY_POUND;
							return (send_data(SendData,LENGTH));
							}							
						break;
			
					case	ITEM_BTMAIN_ITEM_1:
					case	ITEM_BTMAIN_ITEM_2:
					case	ITEM_BTMAIN_ITEM_3:
						DailedNO[length] = '0' +(idx-3);
						length++;
						if (Status_flag )
							{
							SendData = idx -3;
							return (send_data(SendData,LENGTH));
							}	
						break;
					case	ITEM_BTMAIN_ITEM_4:
					case	ITEM_BTMAIN_ITEM_5:
					case	ITEM_BTMAIN_ITEM_6:
						DailedNO[length] =  '0' +(idx-4);
						length++;
						if (Status_flag )
							{
							SendData = idx -4;
							return (send_data(SendData,LENGTH));
							}
						break;
					case	ITEM_BTMAIN_ITEM_7:
					case	ITEM_BTMAIN_ITEM_8:
					case	ITEM_BTMAIN_ITEM_9:
						DailedNO[length] =  '0' +(idx-5);
						length++;
						if (Status_flag )
							{
							SendData = idx -5;
							return (send_data(SendData,LENGTH));
							}						
						break;
					case	ITEM_BTMAIN_ITEM_0:
						DailedNO[i] =  '0';
						length++;						
						if (Status_flag )
							{
							SendData = 0x0a;
							return (send_data(SendData,LENGTH));
							}
						break;	
					
				}
	
}

BOOL BTCallBtnEnter(idx)
{
	int SendData =0;
	switch (idx)
	{
		case	ITEM_BTCALL_ITEM_BTBUT:
			if (TRANSFER_AUD)
			{
				TRANSFER_AUD =0;
				SendData= FUNC_KEY_TRANSFER_AUD_TO_BT;
			}
			else
			{
				TRANSFER_AUD =1;
				SendData = FUNC_KEY_TRANSFER_AUD_TO_CELL;
			}							 
			break;
			
		case	ITEM_BTCALL_ITEM_CALBUT:						
			SendData = FUNC_KEY_ANSWER;
			break;
			
		case	ITEM_BTCALL_ITEM_UNCAL:
			SendData = FUNC_KEY_REJECT;
			break;
	}
	return (send_data(SendData,LENGTH));
}


BOOL BTBookMainBtnEnter(idx)
{
	int SendData =0;
	switch (idx)
	{
		case	ITEM_BTBOOK_MAIN_DIALED:
			SendData = FUNC_KEY_DIALED_CALL;			 
			break;
			
		case	ITEM_BTBOOK_MAIN_RECEV:						
			SendData = FUNC_KEY_RECEIVED_CALL;
			break;
			
		case	ITEM_BTBOOK_MAIN_MISSED:
			SendData = FUNC_KEY_MISSED_CALL;
			break;
	}
	return (send_data(SendData,LENGTH));
}

BOOL BTBookNumBtnEnter(idx)
{
	int length =0;
	int i;

	length = sizeof (PhoneNO[idx]);
	for (i=3; i<length +3; i++)
	send_data_buf [i] = PhoneNO[idx][i-3];
	
	return (send_data(send_data_buf[3],length));
}

BOOL BTToolBtnEnter(idx)
{
	int SendData =0;

	switch (idx)
	{
		case	ITEM_BTTOOL_PAIR_ENTER:
			SendData = FUNC_PAIR;			 
			break;
			
		case	ITEM_BTTOOL_PAIR_EXIT:						
			SendData = FUNC_EXIT_PAIR;
			break;
			
		case	ITEM_BTTOOL_ATANSR_ON:
			SendData = FUNC_AUTO_ANSWER;
			break;
		case	ITEM_BTTOOL_ATANSR_OFF:
				SendData = FUNC_CANCEL_AUTO_ANSWER;			 
				break;
				
		case	ITEM_BTTOOL_ATLINK_ON:						
				SendData = FUNC_AUTO_LINK;
				break;
				
		case	ITEM_BTTOOL_ATLINK_OFF:
				SendData = FUNC_CANCEL_AUTO_LINK;
				break;

		
	}
	return (send_data(SendData,LENGTH));
}



BOOL BTMainBtnEnter(int idx)
{
printf ("in main button handle\n");
	switch(idx)
	{
		case ITEM_BT_PHONE:	
			BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,"",FALSE );
			BTDisp_Flag = 0;
			break;
		case ITEM_BT_SET:
			BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,"",FALSE );
			BTDisp_Flag = 4;
			break;
		case ITEM_BT_BOOK:	
			BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,BOOKMAINBARStr,TRUE );
			BTDisp_Flag = 2;
			break;		
	}
	BTDispDrawSeparateItem(FALSE,DISP_ALL);		
	dt_Present_part(CoverInfoRect);	
	LOG( "btn = %d\n", idx );	
	return TRUE;
}


BOOL BTSeprtBtnEnter(int idx)
{
	printf ("in BTSeprtBtnEnter and idx is %d___\n",idx );

	switch (BTDisp_Flag)
		{
			case 0:				
				BTMainDailBtnEnter(idx);
				BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,DailedNO,TRUE );
				break;
			case 1:
				BTCallBtnEnter(idx);
				Status_flag =0;
				break;
			case 2:
				BTBookMainBtnEnter (idx);
				break;
			case 3:
				BTBookNumBtnEnter(idx);
				break;
			case 4:
				BTToolBtnEnter(idx);
				break;
			
		}
	LOG( "btn = %d\n", idx );	
	return TRUE;
}


void BTDispInit(void)
{
	ir_SetCurrentID( IR_BT_INIT );
	BTDisp_Flag = 0;//BT main
	Status_flag =0;
	frm_Create( &MenuBTFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	BTDispDraw( TRUE );
	printf("in BT init!\n");
	//rece_handle = 0;
}

static int send_data (int data_3,int length)
{

	send_data_buf [3] =  data_3;	
	send_data_buf[0] =	HEAD;		
	send_data_buf[1] =	length; 	
	send_data_buf [2] = IDDATA_BT;		
	return ( send_data_out(send_data_buf,length+2 ));	

		
}

void dt_Present_Separate_part (int idx)
{
	switch (BTDisp_Flag)
		{
			case 0:
				printf ("in dt_Present_part 0\n");
				dt_Present_part(BTMainDailTouchRect [idx-3]);
				break;
			case 1:
				dt_Present_part(BTCallTouchRect [idx-3]);
				break;
			case 2:
				dt_Present_part(BTBookNumTouchRect [idx-3]);
				break;
			case 3:
				dt_Present_part(BTBookNumTouchRect [idx-3]);
				break;
			case 4:
				dt_Present_part(BTToolTouchRect [idx-3]);
				break;
					
		}

	
}



/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;	

	switch (read_info_data[2])
	{
		case	ID_POWER:						
			break;
							
		case	ID_DISPLAY_SCREEN:
			switch (read_info_data[3])
			{
				case	ID_SCREEN_MENU:
					MenuMainInit();
					break;		
				case	ID_SCREEN_RADIO:
					MenuRadioInit();
					break;									
				case	ID_SCREEN_CDLOAD:											
				case	ID_SCREEN_CDDISP:
				case	ID_SCREEN_MP3DISP:
				case	ID_SCREEN_NODISC:
				case	ID_SCREEN_DISCERR:
				case	ID_SCREEN_EJECT:
					MenuCdInit();
					rece_handle = 0;
					break;
				case	ID_SCREEN_MEDIEA:									
					break;
										
				case	ID_SCREEN_NAVI:
					MenuNaviInit();
					break;	
				case		ID_SCREEN_AUDIODISP:
					MenuAudioInit();
					break;	
				case		ID_SCREEN_VIDEODISP:
					MenuVideoInit();
					break;	
				case		ID_SCREEN_EQDISP:
					AudioEQInit();
					break;	
				case		ID_SCREEN_BASTRA:
					AudioBASInit();
					break;	
				case		ID_SCREEN_FADBAL:	
					AudioFADInit(); 						
					break;	
				}
	
				rc = 0;//if handled, clear this flag.	
				read_info_data[0] = 0;			
																										
				break;
							
			case	ID_KEY_STATUS:
				rc = 0;//if handled, clear this flag.	
				read_info_data[0] = 0;						
				break;
			case	ID_SCREEN_INFO: 								
				if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
				{
					BTDispDrawVolume(MenuBTFrame.pCanvas,1);	
				}
				break;
							
			case	ID_KEYPAD:	
				switch (read_info_data[3])
				{
					case	ID_MUTE:
						BTDispDrawVolume(MenuBTFrame.pCanvas,1);		
						break;
					case	ID_DMUTE:	
						BTDispDrawVolume(MenuBTFrame.pCanvas,1);		
						break;									
					case	ID_SEEKUP:
					case	ID_SEEKDN:
					case	ID_FMAM:
						MenuRadioInit();//band command;
						break;	
										
					case	ID_DEST:
					case	ID_NAVI:
						MenuNaviInit();
						break;
										
					case	ID_CD:
						MenuCdInit();
						break;				
					case	ID_MEDIA:
						MenuMediaInit();
						break;
					case	ID_MENU:
						MenuMainInit();
						break;
					case	ID_TVAUX:
											
						break;
					}	
					rc = 0;//if handled, clear this flag.	
					read_info_data[0] = 0;						 
					break;
							
		case	ID_BAND_SEL:
			MenuRadioInit();
			rc = 0;//if handled, clear this flag.	
			read_info_data[0] = 0;			
			break;		
		case	ID_BT_STATUS:
			BTDisp_Flag = 0;
			BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,BTStatusCmd[read_info_data[3]],TRUE );
			read_info_data[0] = 0;
			break;
		case	ID_COMEING_CALL_NUM:
			BTDisp_Flag =1;
			BTDispDrawBarInfoItem(MenuBTFrame.pCanvas,ComingCallNO,TRUE );
			BTDispDrawBTCallItem( MenuBTFrame.pCanvas,DISP_ALL,FALSE);
			read_info_data[0] = 0;
			dtpresent_flag++;		
			pthread_cond_signal(&dt_cond);			
			break;
		case	ID_DIALED_CALL_NUM:
		case	ID_RECEIVED_CALL_NUM:
		case	ID_MISSED_CALL_NUM:
			BTDisp_Flag =3;
			BTDispDrawBTBookNumItem( MenuBTFrame.pCanvas,DISP_ALL,FALSE);
			read_info_data[0] = 0;
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			break;
		default:	
				rc = -1;						
			}
		return 0;					
	}


						

void BTDispDefInput(UInt32 key)
{
		int flag;	
		static int ComButFlag = 0;
	
		if (( key == IRC_READ_INFO) && (read_info_data[1] != 0))
		{			
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}				
		}

	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;

		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if(( key == IRC_TOUCH_DOWN ) && ( LastKey ==IRC_TOUCH_UP ))
		{			
			if( (MenuBTFocus= BTMainGetTouchID( pos )) != -1 )
				{
					BTDispDrawComItem( MenuBTFrame.pCanvas, MenuBTFocus, TRUE );
					dt_Present_part(BTMainTouchRect [MenuBTFocus]);	
			//		if ( send_data(MenuCdFocus))
			//			printf ("write failure !\n");	
					ComButFlag =1;
				}	
			else if( (MenuBTFocus = BTOtherGetTouchID( pos )) != -1 )
				{
					BTDispDrawSeparateItem(TRUE,MenuBTFocus);
					dt_Present_Separate_part(MenuBTFocus);
					ComButFlag =0;
				}	
		}
		else
		{		
			if( ( focus = BTMainGetTouchID( pos ) ) == MenuBTFocus )
				{	
					BTDispDrawComItem( MenuBTFrame.pCanvas, MenuBTFocus, FALSE);
					dt_Present_part(BTMainTouchRect [MenuBTFocus]);
					BTMainBtnEnter( MenuBTFocus );
					Status_flag =0;
					ComButFlag = 0;
				}
			else if( ( focus = BTOtherGetTouchID( pos ) ) == MenuBTFocus )
				{
					BTDispDrawSeparateItem(FALSE,MenuBTFocus);
					dt_Present_Separate_part(MenuBTFocus);
					BTSeprtBtnEnter( MenuBTFocus );
					ComButFlag = 0;
				}	
			else if( MenuBTFocus != -1 )
			{
				if (ComButFlag )
					{
					BTDispDrawComItem( MenuBTFrame.pCanvas, MenuBTFocus, FALSE);
					dt_Present_part(BTMainTouchRect [MenuBTFocus]);
					}
				else
					{
					BTDispDrawSeparateItem(FALSE,MenuBTFocus);
					dt_Present_Separate_part(MenuBTFocus);
					}
				
			}
			MenuBTFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

